
public class e2piece {

    int id;
    int left;
    int top;
    int right;
    int bottom;
    int hilite;
    boolean used;
    boolean isHint = false;
    int type; //0=inner, 1=border 2=corner
    e2position position;

    public boolean is_hint(){
        return isHint;
    }
     public void set_as_hint(){
         isHint = true;
     }
    public e2piece(int in_Id, int in_Top, int in_Right, int in_Bottom, int in_Left) {
        int count_border = 0;
        id = in_Id;
        top = in_Top;
        right = in_Right;
        bottom = in_Bottom;
        left = in_Left;
        if (top == 1) {
            count_border++;
        }
        if (right == 1) {
            count_border++;
        }
        if (bottom == 1) {
            count_border++;
        }
        if (left == 1) {
            count_border++;
        }
        type = count_border;
        hilite = 1;
        position = new e2position(0, 0, 0);
    }

    public boolean is_matching_pattern(e2colorpattern in_pattern) {
        //return true if at least one rotation of the piece is matching the color pattern
        boolean result;
        result = false;
        if (top == in_pattern.top || in_pattern.top == 0) {
            if (right == in_pattern.right || in_pattern.right == 0) {
                if (bottom == in_pattern.bottom || in_pattern.bottom == 0) {
                    if (left == in_pattern.left || in_pattern.left == 0) {
                        result = true;
                    }
                }
            }
        }
        if (result == false) {
            if (right == in_pattern.top || in_pattern.top == 0) {
                if (bottom == in_pattern.right || in_pattern.right == 0) {
                    if (left == in_pattern.bottom || in_pattern.bottom == 0) {
                        if (top == in_pattern.left || in_pattern.left == 0) {
                            result = true;
                        }
                    }
                }
            }
        }
        if (result == false) {
            if (bottom == in_pattern.top || in_pattern.top == 0) {
                if (left == in_pattern.right || in_pattern.right == 0) {
                    if (top == in_pattern.bottom || in_pattern.bottom == 0) {
                        if (right == in_pattern.left || in_pattern.left == 0) {
                            result = true;
                        }
                    }
                }
            }
        }
        if (result == false) {
            if (left == in_pattern.top || in_pattern.top == 0) {
                if (top == in_pattern.right || in_pattern.right == 0) {
                    if (right == in_pattern.bottom || in_pattern.bottom == 0) {
                        if (bottom == in_pattern.left || in_pattern.left == 0) {
                            result = true;
                        }
                    }
                }
            }
        }
        if (result == true) {
            int count_border = 0;
            if (in_pattern.top == 1) {
                count_border++;
            }
            if (in_pattern.right == 1) {
                count_border++;
            }
            if (in_pattern.bottom == 1) {
                count_border++;
            }
            if (in_pattern.left == 1) {
                count_border++;
            }
            if (count_border != type) {
                result = false;
            }
        }
        if (isHint == true) {
        	return false;
        }
        return result;
    }

    public boolean is_matching_pattern_with_rotation(e2colorpattern in_pattern) {
        //return true if the piece with the given rotation is matching the color pattern
        //rotation is counted clockwise : 1 = turn right 1 quarter...and so on 
        boolean result;
        result = false;
        if (in_pattern.rotation == 0) {
            if (top == in_pattern.top || in_pattern.top == 0) {
                if (right == in_pattern.right || in_pattern.right == 0) {
                    if (bottom == in_pattern.bottom || in_pattern.bottom == 0) {
                        if (left == in_pattern.left || in_pattern.left == 0) {
                            //System.out.println("  matching rotation " + in_pattern.rotation);
                            result = true;
                        }
                    }
                }
            }
        } else if (in_pattern.rotation == 1) {
            if (right == in_pattern.top || in_pattern.top == 0) {
                if (bottom == in_pattern.right || in_pattern.right == 0) {
                    if (left == in_pattern.bottom || in_pattern.bottom == 0) {
                        if (top == in_pattern.left || in_pattern.left == 0) {
                            System.out.println("  matching rotation " + in_pattern.rotation);
                            result = true;
                        }
                    }
                }
            }
        } else if (in_pattern.rotation == 2) {
            if (bottom == in_pattern.top || in_pattern.top == 0) {
                if (left == in_pattern.right || in_pattern.right == 0) {
                    if (top == in_pattern.bottom || in_pattern.bottom == 0) {
                        if (right == in_pattern.left || in_pattern.left == 0) {
                            System.out.println("  matching rotation " + in_pattern.rotation);
                            result = true;
                        }
                    }
                }
            }
        } else {
            if (left == in_pattern.top || in_pattern.top == 0) {
                if (top == in_pattern.right || in_pattern.right == 0) {
                    if (right == in_pattern.bottom || in_pattern.bottom == 0) {
                        if (bottom == in_pattern.left || in_pattern.left == 0) {
                            System.out.println("  matching rotation " + in_pattern.rotation);
                            result = true;
                        }
                    }
                }
            }
        }
        if (result == true) {
            int count_border = 0;
            if (in_pattern.top == 1) {
                count_border++;
            }
            if (in_pattern.right == 1) {
                count_border++;
            }
            if (in_pattern.bottom == 1) {
                count_border++;
            }
            if (in_pattern.left == 1) {
                count_border++;
            }
            if (count_border != type) {
                result = false;
            }
        }
        if (isHint == true) {
        	result = false;
        }
        return result;
    }

    public int rotations_matching_pattern(e2colorpattern in_pattern) {
        //return rotations of the piece matching the color pattern
        //The last 4th bits are associated with the 4th rotations
        //if rotation is matching the pattern the bit is at 1
        int result;
        result = 0;
        if (top == in_pattern.top || in_pattern.top == 0) {
            if (right == in_pattern.right || in_pattern.right == 0) {
                if (bottom == in_pattern.bottom || in_pattern.bottom == 0) {
                    if (left == in_pattern.left || in_pattern.left == 0) {
                        result = result | 1;
                    }
                }
            }
        }
        if (right == in_pattern.top || in_pattern.top == 0) {
            if (bottom == in_pattern.right || in_pattern.right == 0) {
                if (left == in_pattern.bottom || in_pattern.bottom == 0) {
                    if (top == in_pattern.left || in_pattern.left == 0) {
                        result = result | 2;
                    }
                }
            }
        }
        if (bottom == in_pattern.top || in_pattern.top == 0) {
            if (left == in_pattern.right || in_pattern.right == 0) {
                if (top == in_pattern.bottom || in_pattern.bottom == 0) {
                    if (right == in_pattern.left || in_pattern.left == 0) {
                        result = result | 4;
                    }
                }
            }
        }
        if (left == in_pattern.top || in_pattern.top == 0) {
            if (top == in_pattern.right || in_pattern.right == 0) {
                if (right == in_pattern.bottom || in_pattern.bottom == 0) {
                    if (bottom == in_pattern.left || in_pattern.left == 0) {
                        result = result | 8;
                    }
                }
            }
        }
        if (result != 0) {
            int count_border = 0;
            if (in_pattern.top == 1) {
                count_border++;
            }
            if (in_pattern.right == 1) {
                count_border++;
            }
            if (in_pattern.bottom == 1) {
                count_border++;
            }
            if (in_pattern.left == 1) {
                count_border++;
            }
            if (count_border != type) {
                result = 0;
            }
        }
        if (isHint == true) {
        	result = 0;
        }
        return result;
    }

    public void rotate(int in_rotate) {
        int tempcolor;
        int i;
        for (i = 0; i < in_rotate; i++) {
            tempcolor = left;
            left = bottom;
            bottom = right;
            right = top;
            top = tempcolor;
            position.rotation++;
            if (position.rotation == 4) {
                position.rotation = 0;
            }

        }
        //System.out.println("rotation of piece"+ id + " is " + position.rotation);

    }

    public void reset_rotation() {
        int tempcolor;
        while (position.rotation != 0) {
            tempcolor = left;
            left = bottom;
            bottom = right;
            right = top;
            top = tempcolor;
            position.rotation++;
            if (position.rotation == 4) {
                position.rotation = 0;
            }

        }
        //System.out.println("rotation of piece"+ id + " is " + position.rotation);

    }

    public e2position get_position() {
        return position;
    }

    public int get_rotation() {
        return position.rotation;
    }

    public void set_position(int i, int j) {
        position.col = j;
        position.row = i;
    }

    public void highlight() {
        hilite = 1;
    }

    public void dehighlight() {
        hilite = 0;
    }

    public boolean isHighlighted() {
        if (hilite == 1) {
        	return true;
        } else {
        	return false;
        }
    }
}



